package com.dataseek.iot.auth.manager.service;

import com.alibaba.fastjson.JSON;
import com.dataseek.iot.core.constant.ResponseConst;
import com.dataseek.iot.core.response.ResponseEntity;
import com.dataseek.iot.mysql.entity.LoginUser;
import com.dataseek.iot.mysql.entity.User;
import com.dataseek.iot.mysql.mapper.MyUserMapper;
import com.dataseek.iot.mysql.mapper.UserMapper;
import com.dataseek.iot.mysql.service.impl.UserServiceImpl;
import com.dataseek.iot.redis.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository
public class UserRepository {

    private static final Map<String, LoginUser> allUsers = new HashMap<>();

    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    UserServiceImpl userService;

    @Autowired
    UserMapper userMapper;


    @Autowired
    RedisUtil redisUtil;



    /**
     * 通过用户名查找
     * @param username
     * @return
     */
    public Optional<LoginUser> findByUsername(String username) {
        LoginUser loginUser = userService.selectAllUserDetails(username);
//        List<Map<String, Object>> result= userService.selectAllUserDetails(user.getUsername(),user.getPassword());
//        if(result.size()==0){
//            return  null;
//        }
//        Map<String, Object> map = result.get(0);
//        LoginUser loginUser = JSON.parseObject(JSON.toJSONString(map), LoginUser.class);
        return Optional.ofNullable(loginUser);
    }


    /**
     * 用户验证
     * @param user
     * @return
     */
    public ResponseEntity validate(User user){
        ResponseEntity responseEntity = new ResponseEntity();
        String username = user.getUsername();
        String password = user.getPassword();
        User existUser = userService.findByUsername(username);
        System.out.println("-----"+ passwordEncoder.encode(password));
        if(Objects.isNull(existUser)){
            responseEntity.setCode(ResponseConst.NO_USERNAME_CODE);
            responseEntity.setMsg(ResponseConst.NO_USERNAME);
            responseEntity.setData("");
        }else if(!passwordEncoder.matches(password, existUser.getPassword())){
            responseEntity.setCode(ResponseConst.ERROR_PASSWORD_CODE);
            responseEntity.setMsg(ResponseConst.ERROR_PASSWORD);
            responseEntity.setData("");
        }else if(existUser.getUsed()==0){
            responseEntity.setCode(ResponseConst.UNUSED_CODE);
            responseEntity.setMsg(ResponseConst.UNUSED);
            responseEntity.setData("");
        }else {
            responseEntity.setCode(ResponseConst.SUCCESS_CODE);
            responseEntity.setMsg(ResponseConst.SUCCESS);
            responseEntity.setData(user);
        }
        return responseEntity;
    }

    /**
     * 用户详情
     * @param authentication
     * @return
     */
    public ResponseEntity userDetail(Authentication authentication){
        org.springframework.security.core.userdetails.User principal = (org.springframework.security.core.userdetails.User) authentication.getPrincipal();
        ResponseEntity entity = new ResponseEntity();
        if(Objects.isNull(principal.getUsername())){
            entity.setCode(ResponseConst.CRUD_FAILED_CODE);
            entity.setMsg(ResponseConst.CRUD_FAILED);
            entity.setData("");
        }
        LoginUser loginUser=  userService.selectAllUserDetails(principal.getUsername());
        if(Objects.isNull(loginUser)){
            entity.setCode(ResponseConst.CRUD_FAILED_CODE);
            entity.setMsg(ResponseConst.CRUD_FAILED);
            entity.setData("");
        }
        entity.setCode(ResponseConst.SUCCESS_CODE);
        entity.setMsg(ResponseConst.SUCCESS);
        entity.setData(loginUser);
        //用户数据放到缓存中
        redisUtil.set(principal.getUsername(), loginUser);
        return entity;
    }



}
